<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const obj = {
            id: 1,
            name: 'andy',
            msg: {
                age: 18,
            },
            color: ['pink', 'red'],
        }

        // 浅拷贝
        // const o = {
        //     ...obj,
        // };
        // for (const key in obj) {
        //     o[key] = obj[key];
        // }
        // console.log(o);

        // 深拷贝
        const o = {};
        function deepClone(newObj, oldObj) {
            for (const k in oldObj) {
                // 判断属性值属于哪种数据类型
                // 1.获取属性值
                const item = oldObj[k];
                //2. 判断这个值是否是数组
                if (item instanceof Array) {
                    newObj[k] = [];
                    deepClone(newObj[k], item);
                } else if (item instanceof Object) {
                    newObj[k] = {};
                    // 3.判断这个值是否是对象
                    deepClone(newObj[k], item);
                } else {
                    // 4.属于简单数据类型
                    newObj[k] = item;
                }
            }
            return o;
        }
        console.log(deepClone(o, obj));
       
    </script>
</body>
</html>
